home *** CD-ROM | disk | FTP | other *** search
- Path: news.ld.centuryinter.net!usenet
- From: dongayle@centuryinter.net (Don & Gayle Peterson)
- Newsgroups: comp.lang.c,comp.unix.programmer
- Subject: Re: Q: '\n' character
- Date: Fri, 12 Apr 1996 05:04:27 GMT
- Organization: Picture Perfect
- Message-ID: <4kko80$8np@news.ld.centuryinter.net>
- References: <4kj66f$k0o@ren.cei.net> <4kjh25INNf2e@anvil.ugrad.cs.ubc.ca>
- NNTP-Posting-Host: anxp2.mn.centuryinter.net
- X-Newsreader: Forte Free Agent 1.0.82
-
- c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku) wrote:
-
- >In article <4kj66f$k0o@ren.cei.net>, <james lemley> wrote:
- >>>>>Is there a function or some sort of way that I could remove '\n'
- >>>>>charecter form the end of the string.
- >>
- >>fgets(buffer, file);
- >>buffer[strlen(buffer)-1]=0;
- >>
- >>
- >>It may not be the fastest, but it is darned near the easiest.
-
- >It's also completely incorrect. What if there is no newline at the end?
-
- >This is just part of a bullshit solution to the original problem which is to
- >print lines from two files side by side.
-
- >In implementing the solution, there is no need to build a string and then try
- >to remove a trailing newline.
-
- >Here is one solution that doesn't use its own buffers at all, though admittedly
- >it does once call feof() on the file streams before anything is read from them
- >for the sake of keeping everything in one tidy loop, rather than splitting
- >into cases:
-
-
- >#include <stdio.h>
-
- >void concatprint(FILE *f1, FILE *f2)
-
- >/*
- > * read lines from f1 and f2 simultaneously, and print them side by side
- > * f1 and f2 must be valid streams open for reading.
- > */
-
- >{
- > int c;
-
- > do {
- > if (!feof(f1) && !ferror(f1))
- > while ((c = getc(f1)) != '\n' && c != EOF)
- > putchar(c);
- > if (!feof(f2) && !ferror(f2))
- > while ((c = getc(f2)) != '\n' && c != EOF)
- > putchar(c);
- > putchar('\n');
- > } while ((!feof(f1) && !ferror(f1)) || (!feof(f2) && !ferror(f2)));
- >}
-
- >This is a complete, tested solution to the original posting, unlike some of the
- >half-baked, unverified remarks that were posted in response, which completely
- >miss the central problem anyway. There were calls for strtok(), fgets() and
- >other nonsense, most of which were shown incorrect, anyway. That's like if I
- >went to a doctor with my own ad-hoc diagnosis, and he prescribed a remedy for
- >what I think is wrong with me instead of diagnosing.
-
- >Patient: ``Doctor, I think I need to strip a newline from a string to make my
- >program work!''
-
- >Doctor: ``I won't bother looking at what your program is trying to do despite
- >that you brought it with you; you obviously know what you are doing, and just
- >need a little pointer. Here is a prescription for removing a newline that will
- >sometimes work...''
- >--
- It's a shame I didn't see the full original posting, but maybe your
- solution is 'bullshit' too.
- A long, long time ago, K&R taught me that the beauty in unix is
- simplicity: don't re-invent a tool that already exists. If all the
- original author wants to do is read 2 (or more) files side by side,
- what's wrong with good old pr?
- or, getting fancier, and comparing them, sdiff?...depending on the
- situation, get nuts, use paste... join...
-
- Getting raw side-by-side output from text files at the shell is as
- simple as:
- pr -m file1 file2 ...
- or
- pr -t -m ....
- if you don't want the pr header.
-
- If this requirement is within C code where the multiple inputs
- screw up the ability to use pipes, then forgive me for not knowing all
- the facts.
- But,
- The other response using strlen isn't 'wrong'; there are lots of
- ways to skin a cat. I have yet to use a version of Unix that will
- blow up on forcing a null into a returned NULL pointer.
- For 18 years, I have coded scary stuff like
- *strrchr(buffer, '\n') = '\0'
- and have yet to have a problem. Try it on your system.
- Any problems?
- The mere nature of the author's request would indicate that
- he is looking at text files, (what sense is there doing side-by-
- sides of binaries?) So, why not a utility that works with, and
- assumes, text? Unix text files have \n terminators. Correct me
- if I'm wrong.
-
- So, why write a bunch of lines of _elegant_ code to simply
- clobber a newline?
-
- I also notice that your solution does not take into account any
- output formatting: what happens to the output from f2 when f1 is
- shorter that f2? From what I see, it starts looking like output from
- f1.
-
- Is your version really faster? Does it really matter for the author's
- needs? Is it easier to follow? Is it smaller? Easier to debug when
- buried in a few thousand lines of code? By the way, isn't the stdio
- library's intent to create more efficient i/o? Is getc/putchar really
- more efficient than fgets or fread?
-
- I'm new to this group, and your type of response is kind of scary:
- does everyone here have a chip on their shoulder?
- Cut others a little slack!
-
-